package com.tarena.lbs.basic.web.repository;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.basic.web.mapper.BusinessMapper;
import com.tarena.lbs.pojo.basic.po.BusinessPO;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class BusinessRepository {
    @Autowired
    private BusinessMapper businessMapper;

    public Long businessTotal(BusinessQuery query) {
        //业务条件拼接
        QueryWrapper<BusinessPO> queryWrapper = generateQuery(query);
        return businessMapper.selectCount(queryWrapper);
    }

    private QueryWrapper<BusinessPO> generateQuery(BusinessQuery query) {
        //根据非空 拼接QueryWrapper
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(query.getBusinessName())) {
            log.info("businessName不为空");
            queryWrapper.like("business_name", query.getBusinessName());
        }
        if (StringUtils.isNotEmpty(query.getBusinessHeadPhone())){
            log.info("businessHeadPhone不为空");
            queryWrapper.eq("business_head_phone",query.getBusinessHeadPhone());
        }
        if (query.getBusinessStatus()!=null){
            log.info("businessStatus不为空");
            queryWrapper.eq("business_status",query.getBusinessStatus());
        }
        //只有start和eq都不为空  才拼接分页条件
        if (query.getStartingTime()!=null && query.getEndTime()!=null){
            log.info("startingTime和endTime不为空");
            queryWrapper.between("entry_time",query.getStartingTime(),query.getEndTime());
        }
        //商家分页查询 数据授权条件 businessId不为空拼接
        if (query.getBusinessId()!=null){
            log.info("businessId不为空");
            queryWrapper.eq("id",query.getBusinessId());
        }
        return queryWrapper;
    }

    public List<BusinessPO> businessPage(BusinessQuery query) {
        //业务条件拼接
        QueryWrapper<BusinessPO> queryWrapper = generateQuery(query);
        int start = (query.getPageNo()-1)*query.getPageSize();
        queryWrapper.last("limit " + start + "," + query.getPageSize()); // 添加空格
        return businessMapper.selectList(queryWrapper);
    }

    public PageInfo<BusinessPO> businessPageHelper(BusinessQuery query) {
        //1.pageHelper开启分页查询(开启后要立刻执行业务的sql查询)
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
//        if (1==0){
//            //为了保证线程中threadLocal数据没有冗余 先清空
//            PageHelper.clearPage();
//            return null;
//        }
        //2.封装业务条件 查询数据
        QueryWrapper<BusinessPO> queryWrapper = generateQuery(query);
        List<BusinessPO> pos = businessMapper.selectList(queryWrapper);
        //3.pageInfo获取所以查询结果 包括 total list pageNum pageSize
        PageInfo<BusinessPO> pageInfo = new PageInfo<>(pos);
        return pageInfo;
    }

    public BusinessPO getBusinessByName(String businessName) {
        //select * from lbs_business where business_name = #{businessName}
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_name", businessName);
        return businessMapper.selectOne(queryWrapper);
    }

    public void save(BusinessPO poParam) {
        businessMapper.insert(poParam);
    }

    public BusinessPO getBusinessById(Integer businessId) {
        return businessMapper.selectById(businessId);
    }
}
